Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multipart/form-data support #167

Merged
merged 2 commits into from
Dec 15, 2014
Merged

Add multipart/form-data support #167

merged 2 commits into from
Dec 15, 2014

Conversation

ixti
Copy link
Member

@ixti ixti commented Nov 17, 2014

Resolves #73

@sferik
Copy link
Contributor

sferik commented Nov 17, 2014

@ixti Am I missing something? Where is HTTP::FormData::File defined?

@ixti
Copy link
Member Author

ixti commented Nov 17, 2014

@sferik it's not ready yet - work in progress :D

@ixti
Copy link
Member Author

ixti commented Nov 17, 2014

@sferik pushed a patch with api draft. With 99% confidence it will stay as it is -- only internals will be implemented properly. FormData::File might get splitted into IO and File actually.

@ixti ixti changed the title [WIP] multipart/form-data multipart/form-data Dec 15, 2014
@ixti ixti changed the title multipart/form-data Add multipart/form-data support Dec 15, 2014
@ixti ixti added Feature and removed WIP labels Dec 15, 2014
@ixti
Copy link
Member Author

ixti commented Dec 15, 2014

Finally! I've did it!!! :D

Moved FormData code into separate gem: https://github.com/ixti/form_data.rb

# Usage inside with HTTP Gem:

HTTP.post(some_url, :form => {
  :username => "ixti",
  :avatar => FormData::File.new("/path/to/file.jpg"),
  :cv => FormData::File.new(io, :filename => "cv.pdf")
})

# Standalone usage:

form = FormData.create({
  :username => "ixti",
  :avatar => FormData::File.new("/path/to/file.jpg"),
  :cv => FormData::File.new(io, :filename => "cv.pdf")
})

@sferik, @tarcieri I have avoided HTTP namespace mostly to shorten File constructor. Although I'm OK to rename gem to be http-form_data or whaever and include HTTP namespace: really don't know what will be better as it is now or with namespace.

@ixti ixti force-pushed the feature/multipart-post branch 2 times, most recently from 42760a7 to 7dc5f13 Compare December 15, 2014 03:54
@ixti
Copy link
Member Author

ixti commented Dec 15, 2014

Notice that form_data is pretty simple right now. In future versions I hope to implement streaming, so that sending files to socket will become possible without prior reading it completely. Also I want to support nested hashes, so that:

FormData.create({
  :foo => { :bar => "baz" }
}).to_s
# => foo[bar]=baz

FormData.create({
  :foo => { :bar => "baz" },
  :img => FormData::File.new("/home/ixti/avatar.png")
}).to_s
# =>
# --boundary
# Content-Disposition: form-data; name="foo[bar]"
#
# baz
# --boundary
# Content-Disposition: form-data; name="img"; filename="avatar.png"
# Content-Type: application/octet-stream
#
# ...
# --boundary--

Revealed and fixed Transfer-Encoding double entry
@tarcieri
Copy link
Member

LGTM 👍

tarcieri added a commit that referenced this pull request Dec 15, 2014
@tarcieri tarcieri merged commit dc6c489 into master Dec 15, 2014
@ixti ixti deleted the feature/multipart-post branch December 15, 2014 21:55
Eric-Guo added a commit to Eric-Guo/wechat that referenced this pull request Jan 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to post file
3 participants